<?php

	require_once APPPATH . 'controllers/base_controllers/base_controller.php';
	
	class sponsor_walker extends Base_controller
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		
		public function login_register( $urlKey, $loginFailed = false )
		{
			$data['title'] = 'Walk/Run for CORD 2011 - Sponsor a Participant';
			$data['view'] = 'walk/sponsor/login_register_view';
			$data['urlKey'] = $urlKey;
			$data['loginFailed'] = $loginFailed;
			
			$this->load->view( 'templates/registration/registration_template', $data );
		}
		
		
		public function login_process( $urlKey )
		{
			$this->load->model('proxies/Security_proxy');
			
			$personId = $this->Security_proxy->authenticate( $this->input->post('userName'), $this->input->post('password') );
			
			if( $personId != 0 )
			{
				$this->session->set_userdata( 'is_logged_in', true );
				$this->session->set_userdata( 'person_id', $personId );
			
				redirect( base_url() . 'index.php/walk/sponsor_walker/sponsor_amount/' . $urlKey . '/' );
			}
			else
			{
				redirect( base_url() . 'index.php/walk/sponsor_walker/login_register/' . $urlKey . '/true/' );
			}
			
			die;
		}
		
		
		public function create_account( $urlKey )
		{
			$data['title'] = 'Walk/Run for CORD 2011 - Sponsor a Participant';
			$data['view'] = 'walk/sponsor/collect_person_view';
			$data['urlKey'] = $urlKey;
			
			$this->load->view( 'templates/registration/registration_template', $data );
		}
		
		
		public function register_person_process( $urlKey )
		{
			$this->load->model('values/Person_value');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$person = new Person_value();
			$person->setPersonName( $this->input->post('userName') );
			$person->setPassword( $this->encrypt->encode($this->input->post('password')) );
			$person->setFirstName( $this->input->post('firstName') );
			$person->setLastName( $this->input->post('lastName') );
			$person->setGender( $this->input->post('gender') );
			$person->setEmail1( $this->input->post('primaryEmail') );
			$person->setPhone1( $this->input->post('homePhoneNumber') );
			$person->setPhone2( $this->input->post('mobilePhoneNumber') );
			$person->setAddress1( $this->input->post('homeAddress1') );
			$person->setAddress2( $this->input->post('homeAddress2') );
			$person->setCity( $this->input->post('city') );
			$person->setState( $this->input->post('state') );
			$person->setZip( $this->input->post('zipCode') );
			
			$insertPerson = FALSE;
			$updatePerson = FALSE;
			$existingPersonId = 0;
			
			$checkPersonExists = $this->Person_proxy->personExists($person);
			if( $checkPersonExists == 0)
			{
				$insertPerson = TRUE;
				$updatePerson = FALSE;
				$existingPersonId = 0;
			}
			else
			{
				$existingPersonId = $checkPersonExists;
				
				if( $this->Person_proxy->personNameIsNull($existingPersonId) )
				{
					$householdXrefId = $this->Person_proxy->personIdInHouseholdXref( $existingPersonId );
					if( $householdXrefId != 0)
					{
						$insertPerson = TRUE;
						$updatePerson = FALSE;
					}
					else
					{
						$insertPerson = FALSE;
						$updatePerson = TRUE;
					}
				}
				else
				{
					$insertPerson = FALSE;
					$updatePerson = TRUE;
				}
				
			}
			
			if( $insertPerson )
			{
				$personId = $this->Person_proxy->insertPerson( $person );
			}
			else if( $updatePerson)
			{
				$person->setPersonId( $existingPersonId );
				$personId = $this->Person_proxy->updatePerson( $person );				
			}
			
			$this->Person_proxy->sendPersonRegistrationEmail( $person );
			
			$this->session->set_userdata( 'is_logged_in', true );
			$this->session->set_userdata( 'person_id', $personId );
			
			redirect( base_url() . 'index.php/walk/sponsor_walker/sponsor_amount/' . $urlKey . '/' );
			
			die;
		}
		
		
		public function sponsor_amount( $urlKey )
		{
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');
			
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $urlKey ));
			
			$data['title'] = 'Walk/Run for CORD 2011 - Sponsor a Participant';
			$data['view'] = 'walk/sponsor/collect_amount_view';
			$data['cwr_registrant_home_value'] = $cwr_registrant_home_value;
			$data['urlKey'] = $urlKey;
			
			$this->load->view( 'templates/registration/registration_template', $data );
		}
		
		
		public function sponsor_amount_summary( $urlKey )
		{
			$this->load->model('values/Sponsor_value');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');
			$this->load->model('proxies/Paypal_proxy');
			
			$sponsorerPersonId = $this->session->userdata( 'person_id' );
			$sponsoreePersonId = $this->Cwr_registrant_member_proxy->getPersonIdFromUrl( $urlKey );
			$sponsorshipAmount = $this->input->post('sponsorshipAmount');
			
			$sponsorValue = new Sponsor_value();
			$sponsorValue->setSponsorerId( $sponsorerPersonId );
			$sponsorValue->setSponsoreeId( $sponsoreePersonId );
			$sponsorValue->setAmount( $sponsorshipAmount );
			
			$paypalTempId = $this->Paypal_proxy->addTempData( serialize($sponsorValue) );
			$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $urlKey ));
			
			$data['title'] = 'Walk/Run for CORD 2011 - Sponsor a Participant';
			$data['view'] = 'walk/sponsor/sponsor_summary_view';
			$data['cwr_registrant_home_value'] = $cwr_registrant_home_value;
			$data['sponsorshipAmount'] = $sponsorshipAmount;
			$data['paypal_form'] = $this->getPayPalForm( $sponsorshipAmount, $paypalTempId );
			
			$this->load->view( 'templates/registration/registration_template', $data );
		}
		
		
		private function getPayPalForm( $amount, $paypalTempId )
		{
			$this->load->library('Paypal_Lib');
			
		    $this->paypal_lib->add_field('return', site_url('/walk/sponsor_walker/success/'));
		    $this->paypal_lib->add_field('cancel_return', site_url('/walk/sponsor_walker/cancel/'));
		    $this->paypal_lib->add_field('notify_url', site_url('/walk/sponsor_walker/ipn/')); // <-- IPN url
		    $this->paypal_lib->add_field('custom', $paypalTempId); // <-- Verify return
	
		    $this->paypal_lib->add_field('item_name', 'CORD Walk 2011 Sponsorship');
		    $this->paypal_lib->add_field('item_number', '');
		    $this->paypal_lib->add_field('amount', $amount);
	
		    return $this->paypal_lib->paypal_form();			
		}
		
		
		public function success()
		{
			$data['title'] = 'Walk/Run for CORD 2010 - Sponsor a Participant Success';
			$data['view'] = 'walk/sponsor/success_view';
			
			$this->load->view( 'templates/registration/registration_template', $data );
		}
		
		
		public function cancel()
		{
			redirect( base_url() );
			die;
		}
		
		
		public function ipn()
		{
			$this->load->library('Paypal_Lib');
			
			if( $this->paypal_lib->validate_ipn() ) 
			{
				$this->load->model('proxies/Walk_proxy');
				$this->load->model('proxies/Paypal_proxy');
				$this->load->model('values/Sponsor_value');
				
				$sponsorValue = unserialize($this->Paypal_proxy->getTempData( $this->paypal_lib->ipn_data['custom'] ));
				$sponsorValue = Sponsor_value::cast( $sponsorValue );
				
				$this->Walk_proxy->insertSponsor( $sponsorValue, $this->paypal_lib->ipn_data['txn_id'] );

				$this->Paypal_proxy->removeTempData( $this->paypal_lib->ipn_data['custom'] );
				
				$this->Walk_proxy->sendWalkSponsorEmail( $sponsorValue );
				
				$this->Walk_proxy->sendWalkSponsoreeEmail( $sponsorValue );
				
				die;
			}
		}
		
		
	}

?>